package com.base.server;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component("rabbitReceiver")
@Slf4j
public class Consumer {

    /**
     * FANOUT广播队列监听一.
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {"FANOUT_QUEUE_A"},containerFactory = "rabbitListenerContainerFactory")
    @Async
    public void on(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("FANOUT_QUEUE_A "+new String(message.getBody()));
    }

    /**
     * FANOUT广播队列监听二.
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception   这里异常需要处理
     */
    @RabbitListener(queues = {"FANOUT_QUEUE_B"})
    @Async
    public void t(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("FANOUT_QUEUE_B "+new String(message.getBody()));
    }

    /**
     * TOPIC模式.
     * 监听同一个队列也会处理
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {"TOPIC_QUEUE1"})
    public void topic(Message message, Channel channel) throws IOException {
        log.info("收到队列[TOPIC_QUEUE1: TOPIC.INFO.*]的消息{}",new String (message.getBody()));
        //消息的标识,false只确认当前一个消息收到，true确认所有consumer获得的消息
        //拒绝消息      channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        //channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }

    /**
     * TOPIC模式.
     * 监听同一个队列也会处理
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {"TOPIC_QUEUE2"})
    public void topic2(Message message, Channel channel) throws IOException {
        log.info("收到队列[TOPIC_QUEUE2: TOPIC.#]的消息{}",new String (message.getBody()));
        //消息的标识,false只确认当前一个消息收到，true确认所有consumer获得的消息
        //拒绝消息      channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        //channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }

    /**
     * DIRECT模式.
     * 监听同一个队列也会处理
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {"DIRECT_QUEUE"})
    public void message(Message message, Channel channel) throws IOException {
        log.info("收到队列[DIRECT_QUEUE]的消息{}",new String (message.getBody()));
        //消息的标识,false只确认当前一个消息收到，true确认所有consumer获得的消息
        //拒绝消息      channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        //channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }

    /**
     * DIRECT模式.
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {"DIRECT_QUEUE"})
    public void message2(Message message, Channel channel) throws IOException {
        log.info("DIRECT2a {}", new String (message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }

}

/*
RabbitListener是Springboot RabbitMq中经常用到的一个注解，将被RabbitListener注解的类和方法封装成MessageListener注入MessageListenerContainer。
（1）当RabbitListener注解在方法上时，对应的方式就是Rabbit消息的监听器。
（2）当RabbitListener注解在类上时，和RabbitHandle注解配合使用，可以实现不同类型的消息的分发，类中被RabbitHandle注解的方法就是Rabbit消息的监听器。
————————————————
版权声明：本文为CSDN博主「翻身已碰头」的原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/qq_39470742/java/article/details/85111910


 */